package req

import (
	"io"
	"logbeat/internal/globalvar"
	"net/http"
)

func Request(method string, url string, body io.Reader) (rep string, status int, err error) {
	req, err := http.NewRequest(method, url, body)
	if err != nil {
		return "", 0, err
	}
	req.SetBasicAuth(globalvar.SysConf.ZincsearchServer.Username, globalvar.SysConf.ZincsearchServer.Password)
	req.Header.Set("Content-Type", "application/json")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return "", 0, err
	}
	defer resp.Body.Close()

	// log.Println(resp.StatusCode)
	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", resp.StatusCode, err
	}
	return string(b), resp.StatusCode, nil
}
